package canss.webservice.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import canss.supports.basic;
import canss.webservice.model.EventDescription;
import canss.webservice.model.Feed;
import canss.webservice.service.PortalIntraWebServiceControl;
import canss.webservice.service.PortalIntraWebServiceControlBean;
import canss.webservice.service.PortalIntraWebService_Struct_Cidstatus;

public class svtOPHome extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet
{

	private static final long serialVersionUID = 219189288676916731L;

	public svtOPHome()
	{
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		doPost(request, response);
	}

	PortalIntraWebServiceControl control;

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		response.setContentType("text/html; charset=UTF-8");

		PrintWriter out = response.getWriter();

		String _notxml = request.getParameter("_notxml");

		control = (control == null) ? new PortalIntraWebServiceControlBean() : control;

		if (_notxml != null)
		{// in html
		} else
		{// in xml
			out.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
			weblogic.application.utils.XMLWriter writer = new weblogic.application.utils.XMLWriter(out);

			writer.addElement("canss");
			writer.addElement("inform");
			{
				/*
				 * person
				 */
				{
					String[] entirepid = control.getEntirePID();
					if(entirepid!=null)
					{
						Integer[] entirepidtype = new Integer[entirepid.length];
						int num_cp = 0;
						int num_3rd = 0;
						for(int i=0; i<entirepid.length; i++)
						{
							entirepidtype[i] = control.getPersonType(entirepid[i]);
							if(entirepidtype[i]!=null)
							{
								if(entirepidtype[i]==basic.DEFAULT_PERSONTYPE_CP)
									num_cp++;
								else if(entirepidtype[i]==basic.DEFAULT_PERSONTYPE_THIRDPARTY)
									num_3rd++;
							}
						}
						writer.addElement("numof_person", ""+(num_cp+num_3rd));
						writer.addElement("numof_cp", ""+num_cp);
						writer.addElement("numof_3rd", ""+num_3rd);
					}
				}
				/*
				 * contents
				 */
				{
					String[] entirecid = control.getCID();
					if(entirecid!=null)
						writer.addElement("numof_contents", ""+entirecid.length);
					PortalIntraWebService_Struct_Cidstatus[] entirecid_pub = control.getCIDByStatus(basic.PUBLISH_STATUS);
					if(entirecid_pub!=null)
						writer.addElement("numof_contents_pub", ""+entirecid_pub.length);
					PortalIntraWebService_Struct_Cidstatus[] entirecid_uploading = control.getCIDByLocationFilteredStatus(1);
					if(entirecid_uploading!=null)
						writer.addElement("numof_contents_uploading", ""+entirecid_uploading.length);
					
					PortalIntraWebService_Struct_Cidstatus[] entirecid_encoding = control.getCIDByLocationFilteredStatus(2);
					if(entirecid_encoding!=null)
					{
						int entirecid_encoded=0;
						for(PortalIntraWebService_Struct_Cidstatus cid : entirecid_encoding)
							if(cid.getStatus()==basic.LOC_ENCODER_2)
								entirecid_encoded++;
						writer.addElement("numof_contents_encoded", ""+entirecid_encoded);
						writer.addElement("numof_contents_encoding", ""+(entirecid_encoding.length-entirecid_encoded));
					}
				}
				/*
				 * feeds
				 */
				{
					Feed[] feeds = control.getEntireFeeds();
					if(feeds!=null)
					{
						writer.addElement("numof_feeds", ""+feeds.length);
						int count = 0;
						for(Feed feed : feeds)
							if(!basic.isEmpty(feed.getRULEID()))
								count++;
						writer.addElement("numof_feeds_ruled", ""+count);
					}
				}
				/*
				 * events
				 */
				{
					EventDescription[] events = control.getEntireEventDesc();
					if(events!=null)
						writer.addElement("numof_events", ""+events.length);
				}
				
			} 
			writer.closeElement();
			writer.finish();
		}
	}
}